package com.clw.template.admin.controller;


import cn.hutool.core.util.EnumUtil;
import com.clw.template.dto.SysPermissionParam;
import com.clw.template.dto.Tree;
import com.clw.template.entity.SysPermission;
import com.clw.template.enums.MenuEnum;
import com.clw.template.service.ISysPermissionService;
import com.clw.template.util.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 权限表 前端控制器
 * </p>
 *
 * @author clw
 * @since 2019-11-01
 */
@Api(tags = "SysPermissionController",description = "权限表")
@RestController
@RequestMapping("/admin/permission")
public class SysPermissionController {

    @Autowired
    private ISysPermissionService sysPermissionService;

    @PreAuthorize("hasAuthority('sys:permission:list')")
    @ApiOperation("查询权限列表")
    @GetMapping("list")
    public CommonResult<SysPermission> list(){
        return sysPermissionService.permissionList();
    }

    @PreAuthorize("hasAuthority('sys:permission:getById')")
    @ApiOperation("获取指定权限")
    @GetMapping("{id}")
    public CommonResult<SysPermission> getById(@PathVariable Long id){
        return CommonResult.success(sysPermissionService.getById(id));
    }

    @PreAuthorize("hasAuthority('sys:permission:add')")
    @ApiOperation("添加权限")
    @PostMapping("add")
    public CommonResult<SysPermission> add(@Validated @RequestBody SysPermissionParam permissionParam){
        SysPermission sysPermission = new SysPermission();
        BeanUtils.copyProperties(permissionParam,sysPermission);
        sysPermissionService.add(sysPermission);
        return CommonResult.success();
    }

    @PreAuthorize("hasAuthority('sys:permission:deleted')")
    @ApiOperation("删除权限")
    @DeleteMapping("{id}")
    public CommonResult<SysPermission> deleted(@PathVariable Long id){
        return sysPermissionService.deleted(id);
    }

    @PreAuthorize("hasAuthority('sys:permission:edit')")
    @ApiOperation("修改权限")
    @PutMapping("{id}")
    public CommonResult edit(@PathVariable Long id,@Validated @RequestBody SysPermissionParam permissionParam){
        SysPermission sysPermission = new SysPermission();
        BeanUtils.copyProperties(permissionParam,sysPermission);
        sysPermission.setId(id);
        return sysPermissionService.edit(sysPermission);
    }

    @PreAuthorize("hasAuthority('sys:permission:getMenuEnum')")
    @ApiOperation("获取所有的类型枚举")
    @GetMapping("getMenuEnum")
    public CommonResult getMenuEnum(){
        return CommonResult.success(EnumUtil.getEnumMap(MenuEnum.class));
    }

    @PreAuthorize("hasAuthority('sys:permission:getTree')")
    @ApiOperation("获取权限树形结构")
    @GetMapping("getTree")
    public CommonResult getTree(){
        List<Tree> trees = new ArrayList<>();
        Tree tree = new Tree();
        tree.setValue(0L);
        tree.setLabel("顶级菜单");
        tree.setChildren(sysPermissionService.permissionTree(0L));
        trees.add(tree);
        return CommonResult.success(trees);
    }

    @PreAuthorize("hasAuthority('sys:permission:getTree2')")
    @ApiOperation("获取权限树形结构")
    @GetMapping("getTree2")
    public CommonResult getTree2(){
        return CommonResult.success( sysPermissionService.permissionTree(0L));
    }

}
